package com.smalls.newvideotwo.utils;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.widget.TextView;

import com.smalls.newvideotwo.service.FloatService;

import java.util.ArrayList;
import java.util.List;

public class NetSpeedUtil {
    private Context context;
    private long curTime;
    public BroadcastReceiver netSpeedReceiver = new BroadcastReceiver() {
        public void onReceive(Context context, Intent intent) {
            if (intent.getAction().equals(FloatService.ActionNetSpeedChanged)) {
                String text = intent.getStringExtra(FloatService.KeyNetSpeedChanged);
                if (NetSpeedUtil.this.textList != null && System.currentTimeMillis() - NetSpeedUtil.this.curTime >= 1000) {
                    NetSpeedUtil.this.curTime = System.currentTimeMillis();
                    for (int i = 0; i < NetSpeedUtil.this.textList.size(); i++) {
                        ((TextView) NetSpeedUtil.this.textList.get(i)).setText(text + "");
                    }
                }
            }
        }
    };
    private List<TextView> textList;

    public NetSpeedUtil(Context context, TextView tv_networkSpeed, TextView tv_speed_buffer) {
        this.context = context;
        if (this.textList == null) {
            this.textList = new ArrayList();
        }
        this.textList.add(tv_speed_buffer);
        this.textList.add(tv_networkSpeed);
    }

    public void addNetSpeedLister(TextView tv) {
        if (this.textList == null) {
            this.textList = new ArrayList();
        }
        this.textList.add(tv);
    }

    public void removeNetSpeedLister(TextView tv) {
        if (this.textList != null) {
            this.textList.remove(tv);
        }
    }

    public void registerNetSpeedReceiver() {
        this.context.registerReceiver(this.netSpeedReceiver, new IntentFilter(FloatService.ActionNetSpeedChanged));
    }

    public void unregisterNetSpeedReceiver() {
        this.context.unregisterReceiver(this.netSpeedReceiver);
    }
}
